//
// Modified version of QDebugStream, a utility class from the qt list archive, to enable colored text
// 
// 

#ifndef QDEBUGSTREAM_H
#define QDEBUGSTREAM_H

#include <QObject>
#include <QPlainTextEdit>
#include <QMutex> 
#include <QString>

/**
*   @class QDebugStream
*   @brief QDebugStream redirects the standard out to a QPlainTextEdit. Specified with a buffer it emulates a debug console functionality
*/
class QDebugStream : public QObject, std::basic_streambuf<char>
{
	Q_OBJECT

public:
    QDebugStream(std::ostream& stream, QPlainTextEdit* text_edit, QString colorCode = "#000000");
    ~QDebugStream();

signals:
   void sendString(QString text);

protected:
	virtual int overflow(int v);
	virtual std::streamsize xsputn(const char* p, std::streamsize n);
    QString createColoredText(const std::string& s);

private:
   std::ostream& m_stream;
   std::streambuf* m_old_buf;
   std::string m_string;
   QPlainTextEdit* log_window;
   QMutex mutex;

   QString m_colorCode;
};

#endif // QDEBUGSTREAM_H
